﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using APUtil;

namespace miniBoxClassLibrary
{
    public class SendVerifyCode
    {
        private APUtil.clsDataSrc oDataSrc;
        private APUtil.clsLog oLog = new APUtil.clsLog();
        private string mSMTP_IP = "";
        private string mSMTP_Uid = "";
        private string mSMTP_Pwd = "";
        private string sResult = "";

        /// <summary>
        /// 寄送SMS驗證碼 
        /// </summary>
        /// <param name="cname"></param>
        /// <param name="email"></param>
        /// <param name="sCheckCode"></param>
        /// <returns></returns>
        public string sendSMS(string cname, string mobile, string sCheckCode,string content)
        {
            MemberClass mc = new MemberClass();
            string account = mc.sSMS_User_Value.Trim();
            account = APUtil.clsCryptoUtil.Decrypt(account);
            string password = mc.sSMS_Pwd_Value.Trim();
            password = APUtil.clsCryptoUtil.Decrypt(password);
            string sign = mc.sSMS_Sign_Value.Trim();
            string pid = mc.sSMS_Pid_Value.Trim();
            mobile = mobile.Trim();
            content = content.Trim();
            sms sms1 = new sms();
            SubmitResult SubmitResult1 = sms1.Submit(account, password, mobile, content);
            return "code:"+ SubmitResult1.code + ",msg:" + SubmitResult1.msg ;
        }

        public String SendSMS(String content, String mobile)
        {
            MemberClass mc = new MemberClass();
            string account = mc.sSMS_User_Value.Trim();
            account = APUtil.clsCryptoUtil.Decrypt(account);
            string password = mc.sSMS_Pwd_Value.Trim();
            password = APUtil.clsCryptoUtil.Decrypt(password);
            //string sign = mc.sSMS_Sign_Value.Trim();
            string pid = mc.sSMS_Pid_Value.Trim();
            mobile = mobile.Trim();
            content = content.Trim();
            //api api1 = new api();
            sms sms1 = new sms();
            SubmitResult SubmitResult1 = sms1.Submit(account, password, mobile, content);
            return "code:"+SubmitResult1.code + ",msg:" + SubmitResult1.msg;
            //Response.Write("<br>code:" + SendSmsResult1.code + "<br>msg:" + SendSmsResult1.msg + "<br>smsid:" + SendSmsResult1.smsid);
        }

        /// <summary>
        /// 寄送電郵驗證碼
        /// </summary>
        public string sendMail(string cname, string email, string sCheckCode)
        {
            try
            {
                oDataSrc = new APUtil.clsDataSrc();
                MemberClass mc = new MemberClass();
                //取得激活認證碼
                //String sCheckCode = new VerifyCode().CheckCode();
                //設定smtp主機
                mSMTP_IP = System.Configuration.ConfigurationSettings.AppSettings["SMTP_IP"];
                mSMTP_Uid = clsCryptoUtil.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["SMTP_UserID"]);
                mSMTP_Pwd = clsCryptoUtil.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["SMTP_Pwd"]);
                SmtpClient mySmtp = new SmtpClient(mSMTP_IP);
                //設定smtp帳密
                mySmtp.Credentials = new System.Net.NetworkCredential(mSMTP_Uid, mSMTP_Pwd);
                //信件內容
                string pcontect = "親愛的" + cname + "會員您好:";
                pcontect = pcontect + "</br><p>感謝您申請成為迷你儲存會員,這是您的激活認證碼:" + sCheckCode + ",為確認您的電郵帳號無誤";
                pcontect = pcontect + "當您收到此認證信後,點擊後方網址輸入您的激活認證碼,激活您的會員帳號</p>";
                pcontect = pcontect + "</br><a href='http://www.google.com'>前往激活網址</a>";
                //設定mail內容
                MailMessage msgMail = new MailMessage();
                //寄件者
                msgMail.From = new MailAddress(mc.sMailFromAddress_Value);
                //收件者
                msgMail.To.Add(email);
                //主旨
                msgMail.Subject = "迷你儲存會員激活認證碼通知信";
                //信件內容(含HTML時)
                AlternateView alt = AlternateView.CreateAlternateViewFromString(pcontect, null, "text/html");
                msgMail.AlternateViews.Add(alt);

                //寄mail
                mySmtp.Send(msgMail);
                sResult = "0";
                //LOG
                oLog.writeLine("寄送驗證碼", "", "", "sendMail", APUtil.clsLog.enumMsgType.General, "收件人:" + email + ",認證碼通知信內容:" + pcontect);
                return sResult;
            }
            catch (Exception ex)
            {
                oLog.writeLine("寄送驗證碼", "", "", "sendMail", APUtil.clsLog.enumMsgType.General, ex.ToString());
                sResult = "-1";
                return sResult;
            }

        }


        /// <summary>
        /// 寄送電郵
        /// </summary>
        /// <param name="email"></param>
        /// <param name="sCheckCode"></param>
        /// <param name="content"></param>
        /// <param name="subject"></param>
        /// <returns></returns>
        public string sendMail(string email, string sCheckCode, string content, string subject)
        {
            try
            {
                oDataSrc = new APUtil.clsDataSrc();
                MemberClass mc = new MemberClass();

                //取得激活認證碼
                //String sCheckCode = new VerifyCode().CheckCode();
                //設定smtp主機
                mSMTP_IP = System.Configuration.ConfigurationSettings.AppSettings["SMTP_IP"];
                mSMTP_Uid = clsCryptoUtil.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["SMTP_UserID"]);
                mSMTP_Pwd = clsCryptoUtil.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["SMTP_Pwd"]);
                SmtpClient mySmtp = new SmtpClient(mSMTP_IP);
                //設定smtp帳密
                mySmtp.Credentials = new System.Net.NetworkCredential(mSMTP_Uid, mSMTP_Pwd);
                //信件內容

                //設定mail內容
                MailMessage msgMail = new MailMessage();
                //寄件者
                msgMail.From = new MailAddress(mc.sMailFromAddress_Value);
                //收件者
                msgMail.To.Add(email);
                //主旨
                msgMail.Subject = subject;
                //信件內容(含HTML時)
                AlternateView alt = AlternateView.CreateAlternateViewFromString(content, null, "text/html");
                msgMail.AlternateViews.Add(alt);

                //寄mail
                mySmtp.Send(msgMail);
                sResult = "0";
                //LOG
                oLog.writeLine("寄送驗證碼", "", "", "sendMail", APUtil.clsLog.enumMsgType.General, "收件人:" + email + ",認證碼通知信內容:" + content);
                mc.sResult_Value = sResult;
                
            }
            catch (Exception ex)
            {
                oLog.writeLine("寄送驗證碼", "", "", "sendMail", APUtil.clsLog.enumMsgType.General, ex.ToString());
                sResult = ex.ToString();
                
            }
            return sResult;
        }
    }
}
